﻿using Common.MyCustomException;
using Common.Result;
using IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.Dto.Dish;
using Model.Dto.Setmeal;
using Model.Entitys;
using Service;

namespace TakeawayWebApi.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class SetmealController : ControllerBase
    {
        private readonly ILogger<SetmealController> _logger;
        private readonly ISetmealService _setmealService;
        public SetmealController(ILogger<SetmealController> logger, ISetmealService setmealService)
        {
            _logger = logger;
            _setmealService = setmealService;
        }
        
        [HttpPost]
        public async Task<ApiResult> Save(SetmealAdd setmealAdd)
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            var res = await _setmealService.SaveWithDishAsync(setmealAdd, userId);
            if (res) return ResultHelper.Success("添加成功");
            return ResultHelper.Error("添加失败");
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet("page")]
        public async Task<ApiResult> GetPageInfo(int page, int pageSize, string? name)
        {
            var res = await _setmealService.GetPageInfoAsync(page, pageSize, name);
            return ResultHelper.Success(res);
        }

        [HttpGet("{id}")]
        public async Task<ApiResult> GetSetmealByTd(long id)
        {
            return ResultHelper.Success(await _setmealService.GetSetmealDtoByTdAsync(id));
        }
        /// <summary>
        /// 根据ids删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> DeleteSetmealByTds(string ids)
        {
            //将ids通过‘，’拆分开合合并成list集合
            List<long> idList = ids.Split(',').Select(long.Parse).ToList();
            return ResultHelper.Success(await _setmealService.DeleteSetmealByIdsAsync(idList));
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        [HttpPut]
        public async Task<ApiResult> EditSetmealByTd(SetmealEdit setmealEdit)
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            var res = await _setmealService.EdiSetmealByIdAsync(setmealEdit, userId);
            if (res) return ResultHelper.Success("修改成功");
            return ResultHelper.Error("修改失败");
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        [HttpPost("status/{status}")]
        public async Task<ApiResult> EditStatusByIds(string ids, int status)
        {
            long userId = Convert.ToInt64(HttpContext.User.Claims.ToList()[0].Value);
            //将ids通过‘，’拆分开合合并成list集合
            List<long> idList = ids.Split(',').Select(long.Parse).ToList();
            var res = await _setmealService.EditStatusByIdsAsync(idList, status, userId);
            if (res) return ResultHelper.Success("修改成功");
            return ResultHelper.Error("修改失败");
        }
        /// <summary>
        /// 根据条件查询套餐数据
        /// </summary>
        [HttpGet("list")]
        public async Task<ApiResult> Getsetmeals([FromQuery]SetmealQuery setmealQuery)
        {
            return ResultHelper.Success(await _setmealService.GetsetmealsAsync(setmealQuery));
        }
    }
}
